package com.lark.web.controller;

import com.github.pagehelper.PageInfo;
import com.lark.common.annotation.Log;
import com.lark.common.core.controller.BaseController;
import com.lark.common.core.domain.R;
import com.lark.common.core.page.PageDomain;
import com.lark.common.enums.BusinessType;
import com.lark.common.utils.poi.ExcelUtil;
import com.lark.framework.cache.DictUtils;
import com.lark.web.domain.Host;
import com.lark.web.model.request.dockerinstall.AddYumRepoRequest;
import com.lark.web.model.request.dockerinstall.InstallRequest;
import com.lark.web.service.DockerInstallService;
import com.lark.web.service.HostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Docker 安装 Controller
 *
 * @author fei
 * @email 952597665@qq.com
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/dockerinsight/dockerinstall")
public class DockerInstallController extends BaseController {

    @Autowired
    private DockerInstallService dockerInstallService;

    /**
     * 查询主机操作系统类型
     */
    @GetMapping("/getHostSystemType/{ip}")
    public R<String> getHostSystemType(@PathVariable("ip") String ip) {
        return R.data(dockerInstallService.getHostSystemType(ip));
    }

    /**
     * centos 添加yum库
     */
    @PostMapping("/addYumRepo")
    public R<String> addYumRepo(@RequestBody @Validated AddYumRepoRequest addYumRepoRequest) {
        return R.data(dockerInstallService.addYumRepo(addYumRepoRequest.getIp(), addYumRepoRequest.getRepo()));
    }

    /**
     * centos 查询可用的Docker版本
     */
    @GetMapping("/getDockerVersion")
    public R<List<String>> getDockerVersion(@RequestParam("ip") String ip) {
        return R.data(dockerInstallService.getDockerVersion(ip));
    }

    /**
     * centos 执行安装
     */
    @PostMapping("/install")
    public R install(@RequestBody @Validated InstallRequest installRequest) {
        return R.data(dockerInstallService.install(installRequest.getIp(), installRequest.getScript()));
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
